home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / BSP Tree 1.2 / Sources / Core / source / event.cp < prev    next >
Encoding:
Text File  |  1995-05-26  |  9.1 KB  |  146 lines  |  [TEXT/MMCC]

  1. //------------------------------------------------------------------------------
  2. //    File:                    event.cp
  3. //    Date:                    7/14/94
  4. //    Author:                Bretton Wade
  5. //
  6. //    Description:    this file contains definitions related to Macintosh event
  7. //                                processing
  8. //
  9. //------------------------------------------------------------------------------
  10.  
  11. #include "event.h"
  12. #include "menu.h"
  13. #include "window.h"
  14.  
  15. //------------------------------------------------------------------------------
  16. //    variables
  17. //------------------------------------------------------------------------------
  18. RgnHandle                gMouseRgn;                                                                                                            //    the mouse moved region
  19. long                        gSleep = 0;                                                                                                            //    how long to sleep between events
  20.  
  21. //------------------------------------------------------------------------------
  22. //    Check whether or not a key is pressed
  23. //------------------------------------------------------------------------------
  24. bool    KeyIsPressed (ushort key)                                                                                                    //    check to see if a key is pressed
  25. {                                                                                                                                                                //    begin
  26.     uchar keymap[16];                                                                                                                            //    keymap
  27.     GetKeys ((ulong *) keymap);                                                                                                        //    get the current keymap
  28.     return bool ((keymap[key >> 3] >> (key & 7)) & 1);                                                        //    return whether or not the requested key is down
  29. }                                                                                                                                                                //    end
  30.  
  31. //------------------------------------------------------------------------------
  32. //    Handle Mouse Click
  33. //------------------------------------------------------------------------------
  34. static    void    HandleClick (EventRecord &event)                                                                    //    deal with a mouse action
  35. {                                                                                                                                                                //    begin
  36.     WindowPtr    wind;                                                                                                                                //    window in which event occured
  37.     short    part = FindWindow (event.where, &wind);                                                                    //    find the window, and what action
  38.     switch (part)                                                                                                                                    //    chech what part of the screen was clicked
  39.     {                                                                                                                                                            //    begin
  40.         case inMenuBar:                                                                                                                            //    the click was in the menu bar
  41.             AdjustMenus ();                                                                                                                        //    be sure the menus are properly adjusted
  42.             HandleMenu (MenuSelect (event.where));                                                                        //    deal with the menu    event
  43.             break;                                                                                                                                        //    end menu bar click
  44.         case inSysWindow:                                                                                                                        //    the click was in a system window
  45.             SystemClick (&event, wind);                                                                                                //    let the system handle it
  46.             break;                                                                                                                                        //    end system window
  47.         default:                                                                                                                                        //    otherwise, the event is a window event
  48.             ((window *) GetWRefCon (wind))->HandleClick (event);                                            //    let the window handle it
  49.             break;                                                                                                                                        //    end window click
  50.     }                                                                                                                                                            //    end
  51. }                                                                                                                                                                //    end
  52.  
  53. //------------------------------------------------------------------------------
  54. //    Handle Key Click
  55. //------------------------------------------------------------------------------
  56. static    void    HandleKey (EventRecord &event)                                                                        //    deal with a key action
  57. {                                                                                                                                                                //    begin
  58.     if (event.modifiers & cmdKey)                                                                                                    //    if the command key is down
  59.         if (HandleMenu (MenuKey (event.message & charCodeMask)))                                        //    this is a menu event
  60.             return;                                                                                                                                        //    we're finished
  61.     WindowPtr    wind = MyFrontWindow ();                                                                                        //    get the front window
  62.     if (wind)                                                                                                                                            //    if there is a front window
  63.         ((window *) GetWRefCon (wind))->HandleKey (event);                                                    //    tell the front window to handle the key event
  64. }                                                                                                                                                                //    end
  65.  
  66. //------------------------------------------------------------------------------
  67. //    HandleOSEvent
  68. //------------------------------------------------------------------------------
  69. static    void    HandleOSEvent (EventRecord &event)                                                                //    handle an event from the OS
  70. {                                                                                                                                                                //    begin
  71.     WindowPtr    wind = MyFrontWindow ();                                                                                        //    get the front window
  72.     if ((event.message & osEvtMessageMask) == 0x01000000)                                                    //    if this is a suspend/resume message
  73.     {                                                                                                                                                            //    begin
  74.         if (event.message & resumeFlag)                                                                                            //    if it is a resume message
  75.         {                                                                                                                                                        //    begin
  76.             if (wind)                                                                                                                                    //    if there is a front window
  77.                 ((window *) GetWRefCon (wind))->Activate (event);                                                //    activate the front window
  78.             gSleep = 0;                                                                                                                                //    set the sleep time to 0
  79.         }                                                                                                                                                        //    end
  80.         else                                                                                                                                                //    otherwise, it is a suspend message
  81.             gSleep = 4;                                                                                                                                //    set the sleep time to allow other apps to run
  82.     }                                                                                                                                                            //    end
  83.     else                                                                                                                                                    //    otherwise, this is a mouse moved event
  84.         if (wind)                                                                                                                                        //    if there is a front window
  85.             ((window *) GetWRefCon (wind))->AdjustCursor (event);                                            //    tell the front window to adjust the cursor
  86.         else                                                                                                                                                //    otherwise, there is no front window
  87.             InitCursor ();                                                                                                                        //    make the cursor an arrow
  88. }                                                                                                                                                                //    end
  89.  
  90. //------------------------------------------------------------------------------
  91. //    Process One Event
  92. //------------------------------------------------------------------------------
  93. void    ProcessOneEvent (void)                                                                                                        //    get one macintosh event and handle it
  94. {                                                                                                                                                                //    begin
  95.     EventRecord event;                                                                                                                        //    event record to be filled in
  96.     WaitNextEvent (everyEvent, &event, gSleep, gMouseRgn);                                                //    fill in the event record
  97.     switch (event.what)                                                                                                                        //    take some action based on the event type
  98.     {                                                                                                                                                            //    begin
  99.         case nullEvent:                                                                                                                            //    there is no event pending
  100.             YieldToAnyThread ();                                                                                                            //    do idle time processing
  101.             break;                                                                                                                                        //    end null event
  102.         case mouseDown:                                                                                                                            //    a mouse click
  103.         //case mouseUp:                                                                                                                                //    a mouse up
  104.             HandleClick (event);                                                                                                            //    deal with the mouse event
  105.             break;                                                                                                                                        //    end mouse event
  106.         case keyDown:                                                                                                                                //    a key press
  107.         case autoKey:                                                                                                                                //    a key held down
  108.             HandleKey (event);                                                                                                                //    deal with the key event
  109.             break;                                                                                                                                        //    end key event
  110.         case activateEvt:                                                                                                                        //    a window needs to be activated/deactivated
  111.             ((window *) GetWRefCon (WindowPtr (event.message)))->Activate (event);        //    activate/deactivate the window
  112.             break;                                                                                                                                        //    end activate
  113.         case updateEvt:                                                                                                                            //    a window needs to be redrawn
  114.             ((window *) GetWRefCon (WindowPtr (event.message)))->Update (event);            //    redraw the window
  115.             break;                                                                                                                                        //    end redraw event
  116.         case osEvt:                                                                                                                                    //    a system event
  117.             HandleOSEvent (event);                                                                                                        //    deal with it
  118.             break;                                                                                                                                        //    end system event
  119.         case diskEvt:                                                                                                                                //    the user has inserted a disk
  120.             if (hiword (event.message))                                                                                                //    check to see if there was an error
  121.             {                                                                                                                                                    //    begin
  122.                 Point    pt = {0, 0};                                                                                                            //    an empty point
  123.                 DIBadMount (pt, event.message);                                                                                    //    initialize the disk
  124.             }                                                                                                                                                    //    end
  125.             break;                                                                                                                                        //    end disk event
  126.         case kHighLevelEvent:                                                                                                                //    apple event
  127.             AEProcessAppleEvent (&event);                                                                                            //    dispatch the event
  128.             break;                                                                                                                                        //    end apple event
  129.     }                                                                                                                                                            //    end
  130. }                                                                                                                                                                //    end
  131.  
  132. //------------------------------------------------------------------------------
  133. //    MouseRegion
  134. //------------------------------------------------------------------------------
  135. void    MouseRegion (RgnHandle region)                                                                                        //    assign the region to the mouse region
  136. {                                                                                                                                                                //    begin
  137.     CopyRgn (region, gMouseRgn);                                                                                                    //    copy the region to the mouse region
  138.     Point    tmpPt, localPt;                                                                                                                    //    places to get region values
  139.     tmpPt = localPt = TopLeft ((*gMouseRgn)->rgnBBox);                                                        //    get corner of bounding box
  140.     LocalToGlobal (&tmpPt);                                                                                                                //    convert to global
  141.     SubPt (localPt, &tmpPt);                                                                                                            //    find  difference
  142.     OffsetRgn (gMouseRgn, tmpPt.h, tmpPt.v);                                                                            //    convert the region to global coordinates
  143. }                                                                                                                                                                //    end
  144.  
  145. //------------------------------------------------------------------------------
  146.